package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

import pailaidai.hc.com.mysvgdemo.R;

/**
 * Created by Administrator on 2018/4/8.
 */

public class RoundStrokeView extends ImageView {
    private static final String TAG = "CirclelmageView";
    private Paint paint;
    private Xfermode xfermode;
    private Path path = new Path();
    private int border;
    private int borderColor;


    public RoundStrokeView(Context context) {
        super(context);
    }

    public RoundStrokeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundStrokeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        path = new Path();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundStrokeView);
        border = a.getDimensionPixelSize(R.styleable.RoundStrokeView_round_border, 0);
        borderColor = a.getColor(R.styleable.RoundStrokeView_round_border_color, Color.RED);
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable mDrawable = getDrawable();
        if (mDrawable == null) {
            super.onDraw(canvas);
        }
        int drawableWidth=mDrawable.getIntrinsicWidth();
        int drawableHeight=mDrawable.getIntrinsicHeight();
        int roundWith=drawableHeight>drawableWidth?drawableWidth:drawableHeight;

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        RectF ovalRect = new RectF(0, 0, roundWith, roundWith);
        int layerld = canvas.saveLayer(getPaddingLeft(), getPaddingTop(), width, height, null, Canvas.ALL_SAVE_FLAG);
        Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
        canvas.drawBitmap(bitmap, new Rect(0, 0, drawableWidth, drawableHeight), ovalRect, null);
        paint.setXfermode(xfermode);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        path.reset();
        path.addOval(ovalRect, Path.Direction.CCW);
        canvas.drawPath(path, paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layerld);
        //画空心圆
        if (border != 0) {
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(borderColor);
            paint.setStrokeWidth(border);
            ovalRect.inset(border / 2, border / 2);
            canvas.drawOval(ovalRect, paint);
        }


    }
}
